package view.editor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import view.Displayable;
import view.View;

import controller.editor.EntityCreationController;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import main.model.Attribute;
import main.model.Entity;

public class EntityCreationView extends View<EntityCreationController> implements Displayable{

	private JFrame frame;
	private JTextField textField;
	private DefaultTableModel attributesToEntity;
	private JTable tablaAtributos;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBox;
	private Entity entity = null;
	private EntityCreationController controller;

	public EntityCreationView() {
		setAttributesToEntity(new DefaultTableModel(){

 		   /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
 		   public boolean isCellEditable(int row, int column) {
				if (column == 0 || column == 1){
					return false;
				}
			   return true;
 		   }
 		});
 		getAttributesToEntity().addColumn ("Attributo");
 		getAttributesToEntity().addColumn ("Cardinalidad");
 		getAttributesToEntity().addColumn("Es Id?");
 		getAttributesToEntity().addColumn("Edit");
 		getAttributesToEntity().addColumn("Delete");
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initialize() {
		setFrame(new JFrame("Crear entidad"));
		getFrame().setBounds(100, 100, 540, 491);
		getFrame().getContentPane().setLayout(null);
		
		JLabel lblNombre = new JLabel("Nombre");
		lblNombre.setBounds(41, 24, 70, 15);
		getFrame().getContentPane().add(lblNombre);
		
		textField = new JTextField();
		textField.setBounds(178, 22, 225, 19);
		getFrame().getContentPane().add(textField);
		textField.setColumns(10);
		setTablaAtributos(new JTable(getAttributesToEntity()));
		JLabel lblAtributos = new JLabel("Atributos");
		lblAtributos.setBounds(41, 168, 70, 19);
		getFrame().getContentPane().add(lblAtributos);
		
		JButton btnAgregarNuevoAtributo_1 = new JButton("Agregar nuevo atributo");
		btnAgregarNuevoAtributo_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonAddAttributes();
			}
		});
		btnAgregarNuevoAtributo_1.setBounds(178, 165, 225, 25);
		getFrame().getContentPane().add(btnAgregarNuevoAtributo_1);
		
		JButton btnTerminar = new JButton("Terminar");
		btnTerminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonTerminar();
			}
		});
		btnTerminar.setBounds(198, 439, 117, 25);
		frame.getContentPane().add(btnTerminar);
		
		JLabel lblTipo = new JLabel("Tipo");
		lblTipo.setBounds(41, 74, 70, 15);
		frame.getContentPane().add(lblTipo);
		comboBox = new JComboBox();
		comboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"Maestra", "Transaccional" }));
		comboBox.setBounds(178, 65, 225, 24);
		frame.getContentPane().add(comboBox);
		
		JScrollPane scrollPane = new JScrollPane(getTablaAtributos());
		scrollPane.setBounds(41, 253, 431, 174);
		frame.getContentPane().add(scrollPane);
	}
	
	public void fillEntityData(){
		if (this.getEntity() != null){
			this.textField.setText(this.getEntity().getName());
			if (this.getEntity().isMasterEntity()){
				comboBox.setSelectedIndex(0);
			} else {
				comboBox.setSelectedIndex(1);
			}
			addAttributesToTable(this.getEntity().getAttributes());
			addAttributesToTable(this.getEntity().getIdAttributes());
		}
	}

	private void addAttributesToTable(List<Attribute> attributes) {
		for (Attribute attribute : attributes) {
			controller.addAttributeToComponent(attribute);
		}
	}

	@Override
	public void show() {
		getFrame().setVisible(true);
	}

	@Override
	public void hide() {
		getFrame().setVisible(false);
	}

	@Override
	public EntityCreationController getController() {
		return this.controller;
	}

	@Override
	public void setController(EntityCreationController controller) {
		this.controller = controller;
	}
	
	public String getTypeSelected(){
		return this.comboBox.getSelectedItem().toString();
	}

	public String getEntityName(){
		return this.textField.getText();
	}
	
	public JFrame getFrame() {
		return frame;
	}

	private void setFrame(JFrame frame) {
		this.frame = frame;
	}

	public JTable getTablaAtributos() {
		return tablaAtributos;
	}

	public void setTablaAtributos(JTable tablaAtributos) {
		this.tablaAtributos = tablaAtributos;
	}

	public DefaultTableModel getAttributesToEntity() {
		return attributesToEntity;
	}

	public void setAttributesToEntity(DefaultTableModel attributesToEntity) {
		this.attributesToEntity = attributesToEntity;
	}

	public void setEntity(Entity entity) {
		this.entity = entity;
	}

	public Entity getEntity() {
		return entity;
	}
}
